package com.cy.dbsys.controller;

import com.cy.dbsys.pojo.SysUser;
import com.cy.dbsys.pojo.SysUserMenu;
import com.cy.dbsys.service.SysMenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class PageController {
    @Autowired
    private SysMenuService sysMenuService;
    //登录页面
    @RequestMapping("/doLoginUI")
    public String doLoginUI(){
        return "login";
    }

    @RequestMapping("/doIndexUI")
    public String doIndexUI(Model model){
        //获取当前用户对象
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();//从session获取登录用户
        //在页面上显示当前用户的名字
        model.addAttribute("username",user.getUsername());

        //获取用户id
        Integer id = user.getId();
        //基于用户id查询用户的菜单信息
        List<SysUserMenu> userMenus = sysMenuService.findUserMenusByUserId(id);
        userMenus.forEach(System.out::println);
        //在页面上动态显示用户访问的菜单
        model.addAttribute("userMenus",userMenus);

        return"starter";
    }

    //基于rest风格提取通用页面跳转方法
    @RequestMapping("/{module}/{moduleUI}")
    public String doMoculeUI(@PathVariable String moduleUI){
        return "sys/"+moduleUI;
    }

    //返回分页页面
    @RequestMapping("/doPageUI")
    public String doPageUI(){
        return "common/page";
    }

//    //呈现菜单页面
//    @RequestMapping("/menu/menu_list")
//    public String doMenuUI(){
//        return "sys/menu_list";
//    }


}
